﻿/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Probel.Ldoc.Shell.Views
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    using Probel.Ldoc.Shell.Properties;
    using Probel.Ldoc.Views.Properties;

    /// <summary>
    /// Interaction logic for SettingsView.xaml
    /// </summary>
    public partial class SettingsView : Window
    {
        #region Constructors

        public SettingsView()
        {
            InitializeComponent();
            this.cbFont.ItemsSource = Fonts.SystemFontFamilies.OrderBy(f => f.Source);

            this.cbFont.SelectedValue = Settings.Default.EditorFontFamily;
            this.cbSize.SelectedValue = Settings.Default.EditorFontSize;

            this.SelectSearchBehaviour();

            if (Settings.Default.SearchOnFirstName && Settings.Default.SearchOnLastName) { this.cbAll.IsChecked = true; }
            else if (Settings.Default.SearchOnFirstName) { this.cbFirstName.IsChecked = true; }
            else { cbLastName.IsChecked = true; }
        }

        #endregion Constructors

        #region Methods

        private void cbFont_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.example.FontFamily = this.cbFont.SelectedItem as FontFamily;
        }

        private void cbSize_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.cbSize.Text == null) { return; }

            int size;
            if (int.TryParse(this.cbSize.Text, out size))
            {
                this.example.FontSize = size;
            }
        }

        private void Click_Cancel(object sender, RoutedEventArgs e)
        {
            CoreSettings.Default.Reload();
            Settings.Default.Reload();
            this.Close();
        }

        private void Click_Ok(object sender, RoutedEventArgs e)
        {
            #region Menu
            CoreSettings.Default.MenuTextVisibility = this.GetValue(this.cb_MenuTextVisibility)
                ? Visibility.Visible
                : Visibility.Collapsed;
            CoreSettings.Default.IsRibbonVisible = this.GetValue(this.cb_RibbonVisible);
            #endregion

            #region Fonts
            Settings.Default.EditorFontSize = this.example.FontSize;
            Settings.Default.EditorFontFamily = this.example.FontFamily;
            #endregion

            #region Search

            if (this.searchBehaviour.SelectedItem is ComboBoxItem)
            {
                var item = this.searchBehaviour.SelectedItem as ComboBoxItem;
                Settings.Default.SearchOnContains = Boolean.Parse(item.Tag.ToString());
            }

            if (this.GetValue(this.cbAll))
            {
                Settings.Default.SearchOnFirstName
                    = Settings.Default.SearchOnLastName
                    = true;
            }
            else
            {
                Settings.Default.SearchOnFirstName = this.GetValue(this.cbFirstName);
                Settings.Default.SearchOnLastName = this.GetValue(this.cbLastName);
            }
            #endregion

            CoreSettings.Default.Save();
            Settings.Default.Save();
            this.Close();
        }

        private bool GetValue(ToggleButton ctrl)
        {
            return (ctrl.IsChecked.HasValue && ctrl.IsChecked.Value);
        }

        private void SelectSearchBehaviour()
        {
            foreach (var item in this.searchBehaviour.Items)
            {
                var current = item as ComboBoxItem;
                if (bool.Parse(current.Tag.ToString()) == Settings.Default.SearchOnContains)
                {
                    this.searchBehaviour.SelectedValue = item;
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.cb_MenuTextVisibility.IsChecked = (CoreSettings.Default.MenuTextVisibility == Visibility.Visible);
            this.cb_RibbonVisible.IsChecked = (CoreSettings.Default.IsRibbonVisible);
        }

        #endregion Methods
    }
}